Hands-on Exercise 3B

Interactivity in Visual Analytics: Principles and Methods

Programming Animated Statistical Graphics with R
Author

patriciatrisno

Published

April 28, 2025

Modified

May 2, 2025

1 Overview

When telling a visually-driven data story, animated graphics tends to attract the interest of the audience and make deeper impression than static graphics. In this hands-on exercise, you will learn how to create animated data visualisation by using gganimate and plotly rpackages. At the same time, you will also learn how to (i) reshape data by using tidyrpackage, and (ii) process, wrangle and transform data by using dplyr package.

1.1 Basic concepts of animation

When creating animations, the plot does not actually move. Instead, many individual plots are built and then stitched together as movie frames, just like an old-school flip book or cartoon. Each frame is a different plot when conveying motion, which is built using some relevant subset of the aggregate data. The subset drives the flow of the animation when stitched back together.

1.2 Terminology

Before we dive into the steps for creating an animated statistical graph, it’s important to understand some of the key concepts and terminology related to this type of visualization.

  1. Frame: In an animated line graph, each frame represents a different point in time or a different category. When the frame changes, the data points on the graph are updated to reflect the new data.

  2. Animation Attributes: The animation attributes are the settings that control how the animation behaves. For example, you can specify the duration of each frame, the easing function used to transition between frames, and whether to start the animation from the current frame or from the beginning.

Before you start making animated graphs, you should first ask yourself: Does it makes sense to go through the effort? If you are conducting an exploratory data analysis, a animated graphic may not be worth the time investment. However, if you are giving a presentation, a few well-placed animated graphics can help an audience connect with your topic remarkably better than static counterparts.

2 Getting Started

pacman::p_load(readxl, gifski, gapminder,
               plotly, gganimate, tidyverse)
  • plotly, R library for plotting interactive statistical graphs.

  • gganimate, an ggplot extension for creating animated statistical graphs.

  • gifski converts video frames to GIF animations using pngquant’s fancy features for efficient cross-frame palettes and temporal dithering. It produces animated GIFs that use thousands of colors per frame.

  • gapminder: An excerpt of the data available at Gapminder.org. We just want to use its country_colors scheme.

  • tidyverse, a family of modern R packages specially designed to support data science, analysis and communication task including creating static statistical graphs.

In this hands-on exercise, the Data worksheet from GlobalPopulation Excel workbook will be used.

col <- c("Country", "Continent")
globalPop <- read_xls("~/Documents/SMU/April Term 2/Visual Analytics/patriciatrisno/ISSS608-VAA/Hands-on_Ex/Hands-on_Ex03/data/GlobalPopulation.xls",
                      sheet="Data") %>%
  mutate_at(col, as.factor) %>%
  mutate(Year = as.integer(Year))

Other way, Instead of using mutate_at()across() can be used to derive the same outputs.

col <- c("Country", "Continent")
globalPop <- read_xls("~/Documents/SMU/April Term 2/Visual Analytics/patriciatrisno/ISSS608-VAA/Hands-on_Ex/Hands-on_Ex03/data/GlobalPopulation.xls",
                      sheet="Data") %>%
  mutate(across(col, as.factor)) %>%
  mutate(Year = as.integer(Year))
  • read_xls() of readxl package is used to import the Excel worksheet.

  • mutate_at()used to apply a function to specific columns selected by name or position.

  • mutate_each_() of dplyr package is used to convert all character data type into factor.

  • mutate of dplyr package is used to convert data values of Year field into integer.

Note: mutate_each_() was deprecated in dplyr 0.7.0. and funs() was deprecated in dplyr 0.8.0. In view of this, we will re-write the code by using mutate_at()

3 Animated Data Visualisation: gganimate methods

gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.

  • transition_*() defines how the data should be spread out and how it relates to itself across time.

  • view_*() defines how the positional scales should change along the animation.

  • shadow_*() defines how data from other points in time should be presented in the given point in time.

  • enter_*()/exit_*() defines how new data should appear and how old data should disappear during the course of the animation.

  • ease_aes() defines how different aesthetics should be eased during transitions.

3.1 Building a static population bubble plot

Basic ggplot2 functions are used to create a static bubble plot, where data points are represented as circles, with their size varying according to a third variable (e.g., population size) to visualize relationships between multiple dimensions of the data

Code
ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(title = "Age Distribution Across Time",
       subtitle = 'Year: {frame_time}',
       x = '% Aged', 
       y = '% Young') 

3.2 Building the animated bubble plot

“Furthermore, the gganimate package allows us to bring our static ggplot2 visualizations to life by adding animation layers. The following code uses gganimate functions to transform a static bubble plot into a dynamic animation, showing how the relationships between variables evolve over time.

Code
ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(title = "Age Distribution Across Time",
       subtitle = 'Year: {frame_time}', 
       x = '% Aged', 
       y = '% Young') +
  transition_time(Year) +       
  ease_aes('linear')          

Code Description

  • transition_time() of gganimate is used to create transition through distinct states in time (i.e. Year).

  • ease_aes() is used to control easing of aesthetics. The default is linear. Other methods are: quadratic, cubic, quartic, quintic, sine, circular, exponential, elastic, back, and bounce.

4 Animated Data Visualisation: plotly

In Plotly R package, both ggplotly() and plot_ly() support key frame animations through the frame argument/aesthetic. They also support an ids argument/aesthetic to ensure smooth transitions between objects with the same id (which helps facilitate object constancy).

In this case, the ids setting helps Plotly know which bubble (or point) is which as it moves from one frame to the next. This makes sure that the same country’s bubble, for example, doesn’t suddenly become a different country’s bubble in the animation

4.1 Building an animated bubble plot: ggplotly()method

By telling ggplotly() which column in our data should be used to create the different frames of the animation (using the framesetting in the aes() of our ggplot() code) and which column uniquely identifies each bubble over time (using the idssetting in aes()). Then, ggplotly() will turn our static bubble plot into a dynamic animation that plays through those frames.

Code
gg <- ggplot(globalPop, 
       aes(x = Old, 
           y = Young, 
           size = Population, 
           colour = Country)) +
  geom_point(aes(size = Population,
                 frame = Year),
             alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(x = '% Aged', 
       y = '% Young')

ggplotly(gg)

This R code uses the ggplot2 and plotly packages to generate an animated bubble plot. It visualizes the relationship between the percentage of older (‘Old’) and younger (‘Young’) populations for different countries over time (‘Year’). The frame = Year aesthetic within geom_point() tells ggplotly() to create animation frames based on the ‘Year’ column, while size = Population controls the bubble size.

Appropriate ggplot2 functions are used to create a static bubble plot. The output is then saved as an R object called gg.

ggplotly() is then used to convert the R graphic object into an animated svg object.

Interactive Tip:

The animated bubble plot above includes a play/pause button and a slider component for controlling the animation. By using the play/pause button and the slider, vwe can:

  • Observe trends over time: See how countries move across the plot as their young and old populations change.

  • Track individual countries: Understand its demographic shifts.

  • Compare countries: Observe the relative positions and sizes of bubbles to compare the age structures and populations of different nations.

  • Identify correlations: Look for potential relationships between the percentage of young and old populations and how these relationships evolve.

Notice that although show.legend = FALSE argument was used, the legend still appears on the plot. To overcome this problem, theme(legend.position='none') see how the code below

Code
gg <- ggplot(globalPop, 
       aes(x = Old, 
           y = Young, 
           size = Population, 
           colour = Country)) +
  geom_point(aes(size = Population,
                 frame = Year),
             alpha = 0.7) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(x = '% Aged', 
       y = '% Young') + 
  theme(legend.position='none')

ggplotly(gg)

Now, the legend on the right side is removed.

4.2 Building an animated bubble plot: plot_ly()method

Now, instead of a static representation and converts it to an animated Plotly object using ggplotly(), we want to enhance the visualization by creating a dynamic animated bubble plot directly using the plot_ly() method. This will allow us to observe how the relationships between variables evolve over time.

Code
bp <- globalPop %>%
  plot_ly(x = ~Old, 
          y = ~Young, 
          size = ~Population, 
          color = ~Continent,
          sizes = c(2, 100),
          frame = ~Year, 
          text = ~Country, 
          hoverinfo = "text",
          type = 'scatter',
          mode = 'markers'
          ) %>%
  layout(showlegend = FALSE)
bp

5 Reference

6 Additional Plots